home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 2 / Meeting Pearls Vol. II (1995)(GTI - Schatztruhe)[!].iso / Pearls / dev / GUI / EAGUI / TextField.c < prev    next >
Text File  |  1994-02-16  |  5KB  |  124 lines

  1. /*********************************************************************************************\
  2. *                                                                                             *
  3. * TextField.c - A simple example source that demonstrates how to implement custom             *
  4. *     images in EAGUI. This source should be included in another source, since it             *
  5. *     does not contain headers and needs some support routines, that are usually              *
  6. *     supplied by a parent environment.                                                       *
  7. *                                                                                             *
  8. * This source is placed in the public domain.                                                 *
  9. *                                                                                             *
  10. * Use a tab size of 5, and a right border of 95 if possible.                                  *
  11. *                                                                                             *
  12. \*********************************************************************************************/
  13.  
  14. /* Alternative alignment flags. If these aren't specified, the default is to
  15.  * center the textfield both horizontally and vertically.
  16.  */
  17. #define CITF_ALIGNLEFT    0x00000001
  18. #define CITF_ALIGNRIGHT    0x00000002
  19. #define CITF_ALIGNTOP    0x00000004
  20. #define CITF_ALIGNBOTTOM    0x00000008
  21.  
  22. /* Information that is needed by this object, but that isn't maintained by EAGUI itself. */
  23. typedef struct ci_TextField
  24. {
  25.     STRPTR            tf_string_ptr;        /* string that is displayed */
  26.     struct TextAttr *    tf_textattr_ptr;    /* font that is used */
  27.     ULONG            tf_flags;            /* different flags */
  28.     UBYTE            tf_frontpen;        /* front pen to use */
  29. };
  30.  
  31. STATIC struct IntuiText itext = {
  32.     1, 0,    /* frontpen, backpen */
  33.     JAM1,    /* drawmode */
  34.     0, 0,    /* left, top offset */
  35.     NULL,    /* textattr */
  36.     NULL,    /* text */
  37.     NULL};    /* next intuitext */
  38.  
  39. /*********************************************************************************************\
  40. *                                                                                             *
  41. * MinSize Method                                                                              *
  42. *                                                                                             *
  43. \*********************************************************************************************/
  44. ULONG meth_MinSize_TextField(struct Hook *hook_ptr, struct ea_Object *obj_ptr, APTR msg_ptr)
  45. {
  46.     ULONG                minwidth, minheight;
  47.     struct ci_TextField *    tf_ptr;
  48.  
  49.     /* get a pointer to our structure, and check if we actually got it */
  50.     if (tf_ptr = (struct ci_TextField *)ea_GetAttr(obj_ptr, EA_UserData))
  51.     {
  52.         /* now, we use the library to determine the dimensions of the string */
  53.         minwidth = ea_TextLength(tf_ptr->tf_textattr_ptr, tf_ptr->tf_string_ptr, 0);
  54.         minheight = ea_TextHeight(tf_ptr->tf_textattr_ptr);
  55.  
  56.         /* and finally, we set these values */
  57.         ea_SetAttr(obj_ptr, EA_MinWidth, minwidth);
  58.         ea_SetAttr(obj_ptr, EA_MinHeight, minheight);
  59.     }
  60.  
  61.     /* we always return success */
  62.     return(0);
  63. }
  64.  
  65. /*********************************************************************************************\
  66. *                                                                                             *
  67. * Render Method                                                                               *
  68. *                                                                                             *
  69. \*********************************************************************************************/
  70. ULONG meth_Render_TextField(struct Hook *hook_ptr, struct ea_Object *obj_ptr, struct ea_RenderMessage *rm_ptr)
  71. {
  72.     struct ci_TextField *    tf_ptr;
  73.     ULONG                minwidth, minheight, width, height;
  74.     ULONG                left, top;
  75.  
  76.     /* get a pointer to our structure, and check if we actually got it */
  77.     if (tf_ptr = (struct ci_TextField *)ea_GetAttr(obj_ptr, EA_UserData))
  78.     {
  79.         /* get sizes of the object */
  80.         ea_GetAttrs(obj_ptr,
  81.             EA_MinWidth,        &minwidth,
  82.             EA_MinHeight,        &minheight,
  83.             EA_Width,            &width,
  84.             EA_Height,        &height,
  85.             TAG_DONE);
  86.  
  87.         /* get offsets of object relative to root (window) */
  88.         left = ea_GetObjectLeft(rm_ptr->root_ptr, obj_ptr);
  89.         top = ea_GetObjectTop(rm_ptr->root_ptr, obj_ptr);
  90.  
  91.         /* now align the object */
  92.         if (tf_ptr->tf_flags & CITF_ALIGNRIGHT)
  93.         {
  94.             left += (width - minwidth);
  95.         }
  96.         else if (!(tf_ptr->tf_flags & CITF_ALIGNLEFT))
  97.         {
  98.             left += (width - minwidth) / 2;
  99.         }
  100.         if (tf_ptr->tf_flags & CITF_ALIGNBOTTOM)
  101.         {
  102.             top += (height - minheight);
  103.         }
  104.         else if (!(tf_ptr->tf_flags & CITF_ALIGNTOP))
  105.         {
  106.             top += (height - minheight) / 2;
  107.         }
  108.  
  109.         /* and finally render it */
  110.         itext.ITextFont = tf_ptr->tf_textattr_ptr;
  111.         itext.IText = tf_ptr->tf_string_ptr;
  112.         itext.FrontPen = tf_ptr->tf_frontpen;
  113.         PrintIText(rm_ptr->rastport_ptr, &itext, left, top);
  114.     }
  115.     /* return success */
  116.     return(0);
  117. }
  118.  
  119. /*********************************************************************************************\
  120. *                                                                                             *
  121. * The end!                                                                                    *
  122. *                                                                                             *
  123. \*********************************************************************************************/
  124.